package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.constant.ReportDataType;
import com.tbit.uqbike.object.pojo.statistics.LowBatteryHour;
import com.tbit.uqbike.object.pojo.statistics.ReportDataDaySta;
import com.tbit.uqbike.service.business.ProportionService;
import com.tbit.uqbike.webmanager.business.vehicle_operation_report.dao.stat.LowBatteryHourDao;
import com.tbit.uqbike.webmanager.dao.stat.ReportDataDayStaDao;
import com.tbit.utils.DateTimeUtil;
import com.tbit.utils.NullUtils;
import com.tbit.utils.ReflectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Author HZW
 * @ClassName ProportionServiceImpl
 * @Description
 * @date 2022/2/22 17:20
 * @Version 1.0
 */
@Slf4j
@Service("proportionService")
public class ProportionServiceImpl implements ProportionService {
    private static final Pattern pattern = Pattern.compile(":");
    @Resource
    private ReportDataDayStaDao reportDataDayStaDao;
    @Resource
    private LowBatteryHourDao lowBatteryHourDao;

    public static <K extends Comparable<? super K>, V> void main(String[] args) {
        Map<String, Object> listMap = new HashMap<>();
        listMap.put("2022-03-05", 123);
        listMap.put("2022-03-03", 123);
        listMap.put("2022-03-06", 123);

        sortByKey(listMap).entrySet().forEach(System.out::println);


        Map<String, Object> sortList = listMap.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.
                        toMap(Map.Entry::getKey, Map.Entry::getValue,
                                (oldVal, newVal) -> oldVal, LinkedHashMap::new));

        sortList.entrySet().forEach(System.out::println);
    }

    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();

        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByKey()
                ).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }

    @Override
    public List<ReportDataDaySta> queryPage(Integer accountId, Long startTime, Long endTime, String lowBatteryTime, String lowEndBatteryTime) throws ParseException {
        Map<String, Object> param = new HashMap<>(5);
        Map<String, Object> map = new HashMap<>(3);
        param.put("accountId", accountId);
        param.put("startTime", new Date(startTime));
        param.put("endTime", new Date(endTime));
        /**将传来的低电时间分割*/
        List<Integer> lowTimeList = pattern.splitAsStream(lowBatteryTime).map((x) -> Integer.parseInt(x))
                .collect(Collectors.toList());
        /**将传来的低电时间分割*/
        List<Integer> lowEndTimeList = pattern.splitAsStream(lowEndBatteryTime).map(Integer::parseInt)
                .collect(Collectors.toList());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> findTimeList = DateTimeUtil.findDaysStr(simpleDateFormat.format(new Date(startTime)), simpleDateFormat.format(new Date(endTime)));
        Map<String, List<ReportDataDaySta>> listMap = new HashMap<>();
        Map<String, LowBatteryHour> lowBatteryHourMap = new HashMap<>();
        Map<String, LowBatteryHour> lowEndBatteryHourMap = new HashMap<>();
        Map<String, LowBatteryHour> lowBatteryHourSevenMap = new HashMap<>();
        Map<String, LowBatteryHour> lowBatteryHalfPastFourteenMap = new HashMap<>();

        for (String findTime : findTimeList) {
            map.put("accountId", accountId);
            /**自定义时间查看低电车辆占比*/
            map.put("startDate", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), lowTimeList.get(0), lowTimeList.get(1)));
            map.put("endDate", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), lowTimeList.get(0), lowTimeList.get(1) + 1));
            LowBatteryHour lowBatteryHour = lowBatteryHourDao.getMachineTotal(map);
            if (!NullUtils.isEmpty(lowBatteryHour) && !NullUtils.isEmpty(lowBatteryHour.getCompleteEta())) {
                /**查询自定义时间内的低电车辆*/
                lowBatteryHourMap.put(findTime, lowBatteryHour);
            }

            /**自定义时间查看低电车辆占比 end*/
            map.put("startDate", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), lowEndTimeList.get(0), lowEndTimeList.get(1)));
            map.put("endDate", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), lowEndTimeList.get(0), lowEndTimeList.get(1) + 1));
            LowBatteryHour lowEndBatteryHour = lowBatteryHourDao.getMachineTotal(map);
            if (!NullUtils.isEmpty(lowEndBatteryHour) && !NullUtils.isEmpty(lowEndBatteryHour.getCompleteEta())) {
                /**查询自定义时间内的低电车辆*/
                lowEndBatteryHourMap.put(findTime, lowEndBatteryHour);
            }

            /**查询低电车辆占比(07:30)的比例*/
            map.put("startDate", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), 7, 30));
            map.put("endDate", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), 7, 31));
            LowBatteryHour lowBatteryHourSeven = lowBatteryHourDao.getMachineTotal(map);
            if (!NullUtils.isEmpty(lowBatteryHourSeven) && !NullUtils.isEmpty(lowBatteryHourSeven.getCompleteEta())) {
                /**查询自定义时间内的低电车辆*/
                lowBatteryHourSevenMap.put(findTime, lowBatteryHourSeven);
            }

            /**查询低电车辆占比(14:30)的比例*/
            map.put("startDate", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), 14, 30));
            map.put("endDate", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), 14, 31));
            LowBatteryHour HalfPastFourteen = lowBatteryHourDao.getMachineTotal(map);
            if (!NullUtils.isEmpty(HalfPastFourteen) && !NullUtils.isEmpty(HalfPastFourteen.getCompleteEta())) {
                /**查询自定义时间内的低电车辆*/
                lowBatteryHalfPastFourteenMap.put(findTime, HalfPastFourteen);
            }

            /**查询时间内的车辆运营数据*/
            map.put("startTime", simpleDateFormat.parse(findTime));
            map.put("endTime", DateTimeUtil.getDate(simpleDateFormat.parse(findTime), 23, 59));
            List<ReportDataDaySta> dataDayStaList = reportDataDayStaDao.getMachineTotal(map);
            listMap.put(findTime, dataDayStaList);
        }

        List<ReportDataDaySta> reportDataDayStaList = new LinkedList<>();
        /**将Map的值排序*/
        sortByKey(listMap).forEach((key, value) -> {
            ReportDataDaySta reportDataDaySta = new ReportDataDaySta();
            try {
                reportDataDaySta.setDataDate(simpleDateFormat.parse(key));
            } catch (ParseException e) {
                log.error(e.getMessage(), e);
            }
            /**查询低电车辆占比(自定义)的比例*/
            if (lowBatteryHourMap.containsKey(key)) {
                reportDataDaySta.setLowVoltage(lowBatteryHourMap.get(key).getCompleteEta() + "%");
            }
            /**查询低电车辆占比(自定义)的比例 end*/
            if (lowEndBatteryHourMap.containsKey(key)) {
                reportDataDaySta.setLowEndVoltage(lowEndBatteryHourMap.get(key).getCompleteEta() + "%");
            }
            /**查询低电车辆占比(07:30)的比例*/
            if (lowBatteryHourSevenMap.containsKey(key)) {
                reportDataDaySta.setLowBatteryHalfPastSeven(lowBatteryHourSevenMap.get(key).getCompleteEta() + "%");
            }
            /**查询低电车辆占比(14:30)的比例*/
            if (lowBatteryHalfPastFourteenMap.containsKey(key)) {
                reportDataDaySta.setLowBatteryHalfPastFourteen(lowBatteryHalfPastFourteenMap.get(key).getCompleteEta() + "%");
            }
            for (ReportDataDaySta dataDaySta : value) {
                if (dataDaySta.getType().equals(ReportDataType.DAILY_MACHINE)) {
                    reportDataDaySta.setMachineNum(dataDaySta.getCompleteEta() + "%");
                }
                if (dataDaySta.getType().equals(ReportDataType.DAILY_MAINTAIN)) {
                    reportDataDaySta.setMaintain(dataDaySta.getCompleteEta() + "%");
                }
                if (dataDaySta.getType().equals(ReportDataType.DAILY_DISPATCH)) {
                    reportDataDaySta.setDispatchNum(dataDaySta.getCompleteEta() + "%");
                }
                if (dataDaySta.getType().equals(ReportDataType.DAILY_SUPERZONE)) {
                    reportDataDaySta.setSuperzone(dataDaySta.getCompleteEta() + "%");
                }
            }
            reportDataDaySta.setDataDateStr(simpleDateFormat.format(reportDataDaySta.getDataDate()));
            /**将字符类型的null补空字符串*/
            reportDataDayStaList.add((ReportDataDaySta) ReflectUtils.checkNull(reportDataDaySta));
        });
        return reportDataDayStaList;

    }

}
